package com.nulldev.util.web.nTube;

import java.io.ByteArrayInputStream;
import java.security.KeyStore;
import java.security.Security;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;

import com.nulldev.util.JVM.JVM;
import com.nulldev.util.VariableAPI.ArrayUtils;
import com.nulldev.util.data.Base64.Base64;
import com.nulldev.util.manage.MarkedForRemoval;
import com.nulldev.util.web.HttpServer3.HttpServer3;
import com.nulldev.util.web.HttpServer3.SSLBehaviour;
import com.nulldev.util.web.HttpServer3.extensions.envStats.EMAPIStatsPage;
import com.nulldev.util.web.HttpServer3.extensions.envStats.ExperimentStatsPage;
import com.nulldev.util.web.HttpServer3.extensions.envStats.HTTPStatsPage;
import com.nulldev.util.web.HttpServer3.extensions.envStats.IOBuffersStatsPage;
import com.nulldev.util.web.HttpServer3.extensions.envStats.TCPDebugPage;
import com.nulldev.util.web.nTube.util.INTubeInstance;

@MarkedForRemoval(reason = "This is a temporary class for testing purposes only.")
public class BLNKNTServer {

	public static void main(String[] args) throws Exception {
		try {
			try {
				Security.setProperty("jdk.tls.disabledAlgorithms", "");
			} catch (Exception e) {
				JVM.errorln(e);
			}
			try {
				final String[] protocols = SSLContext.getDefault().getSupportedSSLParameters().getProtocols();
				System.setProperty("https.protocols", ArrayUtils.join(protocols, ","));
			} catch (Exception e) {
				if (JVM.version() >= 11f) {
					System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2,TLSv1.3");
				} else {
					System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
				}
			}
			final KeyStore ks = KeyStore.getInstance("JKS");
			ks.load(new ByteArrayInputStream(Base64.decodeBase64(INTubeInstance.JKS_SSL_CONTAINER)), "changeit".toCharArray() /* Make me. */);
			final KeyManagerFactory km = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
			km.init(ks, "ntube_1_5_basic_ssl".toCharArray()); // Very Secure, much wow.
			final HttpServer3 e = new HttpServer3(9000, true, SSLBehaviour.USE_NEAREST_PORT_POSITIVE, km.getKeyManagers());
			e.logger().traceEnabled.set(true);
			e.logger().debugEnabled.set(e.logger().traceEnabled.get());
			e.isTimingDebugEnabled = true;
			e.addPage(new EMAPIStatsPage());
			e.addPage(new IOBuffersStatsPage());
			e.addPage(new TCPDebugPage());
			e.addPage(new HTTPStatsPage());
			e.addPage(new ExperimentStatsPage());
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
